import type { ConfigState } from '@/types/hooks/useEdit'
import type { TreeSelectOption } from 'naive-ui'
import type { OptionsType } from '@/types/hooks/editDia.ts'
import { useEdit } from '@/hooks/useEdit.ts'
import { createRules } from './rules.ts'
import { createFormItems } from './formItems.ts'
import { usePageConfig } from '../index.ts'
import routerApi from '@/service/api/router.ts'

export function useAuthEdit(options: OptionsType = {}) {
  const pageConfig = usePageConfig()
  const treeList = ref<TreeSelectOption[]>()
  routerApi.allList().then(({ data }) => {
    treeList.value = data
  })
  const config: ConfigState = {
    ...pageConfig.edit.auth,
    props: {
      formItems: createFormItems({
        treeList,
      }),
      rules: createRules(),
    },
  }
  if (!config?.key) {
    config.key = pageConfig.KEY || 'id'
  }
  const res = useEdit({
    ...config,
    ...options,
  })
  const open = (row) => {
    res.open(row, '授权')
  }
  return {
    open,
  }
}
